﻿using System;
using System.Windows.Controls;
using System.Windows.Navigation;
using CeskyRozhlasWP8.Model;
using CeskyRozhlasWP8.Resources;
using CeskyRozhlasWP8.ViewModel;
using Microsoft.Phone.Shell;
using WP8DataModel.ViewModel;

namespace CeskyRozhlasWP8.View
{
    public partial class RecordingPlanPage
    {
        private readonly BackgroundRecordingPlanViewModel _backgroundRecordingPlanVm = new BackgroundRecordingPlanViewModel();

        public RecordingPlanPage()
        {
            InitializeComponent();
            DataContext = _backgroundRecordingPlanVm;
            ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Text = AppResources.DeleteAllCompletedDownloadsMenuItemText;
        }

        protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            _backgroundRecordingPlanVm.BackgroundRecordingPlan = await BackgroundScheduleModel.Instance.GetTransfersStatus();
            foreach (var item in _backgroundRecordingPlanVm.BackgroundRecordingPlan)
            {
                item.DownloadStatus = BackgroundTransferManager.GetDownloadStatus(item);
            }
            base.OnNavigatedTo(e);
        }

        private void RecordingPlanItemSelected(object sender, SelectionChangedEventArgs e)
        {

        }

        private void DeleteAllCompleted(object sender, EventArgs e)
        {
            _backgroundRecordingPlanVm.DeleteAllCompleted(sender ,e);
        }

        private void SetCheckEnabled(object sender, EventArgs e)
        {
            _backgroundRecordingPlanVm.SelectionVisibility = System.Windows.Visibility.Visible;
            var appBarButton = new ApplicationBarIconButton(new Uri("/Images/delete.png", UriKind.Relative))
            {
                Text = "smazat"
            };
            appBarButton.Click += DeleteAllCompleted;

            ApplicationBar.Buttons.RemoveAt(0);
            ApplicationBar.Buttons.Insert(0, appBarButton);
        }
    }
}